package can.bishe.modules.senic.controller;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

import can.bishe.common.utils.PageUtils;
import can.bishe.common.utils.R;
import can.bishe.modules.senic.entity.SenicInfoEntity;
import can.bishe.modules.senic.entity.SenicScoresEntity;
import can.bishe.modules.senic.service.SenicInfoService;
import can.bishe.modules.senic.service.SenicScoresService;
import can.bishe.modules.sys.controller.AbstractController;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import io.swagger.models.auth.In;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import can.bishe.common.utils.CoreAlgorithm;


/**
 * 景点信息
 *
 * @author chenshun
 * @email sunlightcs@gmail.com
 * @date 2021-03-27 20:23:41
 */
@RestController
@RequestMapping("senic/senicinfo")
public class SenicInfoController extends AbstractController {
    @Autowired
    private SenicInfoService senicInfoService;
    @Autowired
    private SenicScoresService senicScoresService;
    @Autowired
    private CoreAlgorithm coreAlgorithm;

    private final Logger log = LoggerFactory.getLogger(getClass());
    /**
     * 列表
     */
    @RequestMapping("/list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = senicInfoService.queryPage(params);

        return R.ok().put("page", page);
    }

    /**
     * 推荐列表
     */
    @RequestMapping("/recommendlist")
    public R recommendList(@RequestParam Map<String, Object> params){
    	// 推荐算法
//    	if(getUserId()!=null){
//    		Long userId = getUserId();
//    		List<Long> userLoveList = senicScoresService.getUserLoveList(userId);
//        	List<Long> res = coreAlgorithm.getUserToSenicModel(userLoveList);
//        	if(res.size() < 6) { // 不够推荐
//            	List<SenicInfoEntity> recomList = senicInfoService.recomList(null);
//            	System.out.println(res.size());
//        		return R.ok().put("data", recomList);
//        	}else {
//        		List<SenicInfoEntity> recomList = new ArrayList<>();
//        		for(Long index:res) {
//        			SenicInfoEntity entityOne = senicInfoService.selectById(index);
//        			recomList.add(entityOne);
//        		}
//        		return R.ok().put("data", recomList);
//        	}
//    	}
    	List<SenicInfoEntity> recomList = new ArrayList<>();
    	try {
    		Long userId = getUserId();
    		List<Long> userRecomList =coreAlgorithm.getSenicUserModel2(userId);
    		if(userRecomList.size() < 6) {
    			for(Long ids:userRecomList) {
    	   			SenicInfoEntity senicInfoEntitsOne =  senicInfoService.getSenicInfo(ids);
        			recomList.add(senicInfoEntitsOne);
    			}
    			// 补充剩余的景区
    			Integer nums = 6 - userRecomList.size();
    			List<SenicInfoEntity> recomData = senicInfoService.recomList(nums);
    			recomList.addAll(recomData);
    			return R.ok().put("data", recomList);
    		}else {
    			for(Long ids:userRecomList) {
    	   			SenicInfoEntity senicInfoEntitsOne =  senicInfoService.getSenicInfo(ids);
        			recomList.add(senicInfoEntitsOne);
        			if(recomList.size() >=6) {
        				break;
        			}
    			}
    			return R.ok().put("data", recomList);
    		}
    	} catch (NullPointerException e) {
    		System.out.println("用户没有登录推荐默认列表");
    	}
    	recomList = senicInfoService.recomList(6);
		return R.ok().put("data", recomList);
    }
    /**
     * 全部列表
     */
    @RequestMapping("/allInfolist")
    public R allInfoList(@RequestParam Map<String, Object> params){
    	params.get("placeName");
    	if( params.get("placeName")!= null && params.get("senicName") != null) {
    	  	String senicPlace = params.get("placeName").toString();
        	String senicName = params.get("senicName").toString();
        	System.out.println(senicPlace + senicName);
        	List<SenicInfoEntity> data = senicInfoService.allInfoList(senicPlace,senicName);
        	return R.ok().put("data", data);
    	}else {
    		List<SenicInfoEntity> data = senicInfoService.allInfoList(null,null);
    		return R.ok().put("data", data);
    	}
    }
    /**
     * 首页信息汇总接口
     */
    @RequestMapping("/allView")
    public R getAllInfo(@RequestParam Map<String, Object> params){

        List<SenicInfoEntity> allinfoList = senicInfoService.allInfoList(null,null);
        List<String> allPlaceInfoList = senicInfoService.allPlaceInfoList();
        List<SenicInfoEntity> rankList = senicInfoService.selectList(new EntityWrapper<SenicInfoEntity>().orderDesc(Arrays.asList(new String[] {"senic_score"})).last("limit 5"));
        List<SenicInfoEntity> recomList;
        try {
            Long userId = getUserId();
            List<SenicScoresEntity> mylikelist = senicScoresService.selectList(new EntityWrapper<SenicScoresEntity>().eq("user_id",userId));
            int MylikeListSize = mylikelist.size();
            List<Integer> myLikeListId = new ArrayList<>();
            for(SenicScoresEntity scOne:mylikelist){
                myLikeListId.add(scOne.getSenicId());
            }
            if(MylikeListSize == 0 ){
                recomList = senicInfoService.recomList(6);
                log.info("新用户推荐 ");
                return R.ok()
                        .put("allInfoList", allinfoList)
                        .put("allPlaceInfoList",allPlaceInfoList)
                        .put("rankList",rankList)
                        .put("recomList",recomList);
            }
            List<Integer> helikelist = coreAlgorithm.getSenicUserModel3(userId);
            EntityWrapper ew1 = new EntityWrapper<SenicInfoEntity>();
            ew1.in("id",helikelist);
            ew1.notIn("id",myLikeListId);
            ew1.last("limit 6");
            List<SenicInfoEntity> recomlistOne = senicInfoService.selectList(ew1);
            List<SenicInfoEntity> recomlisttwo = new ArrayList<SenicInfoEntity>();
            if(recomlistOne.size() < 6){
                EntityWrapper ew2 = new EntityWrapper<SenicInfoEntity>();
                helikelist.addAll(myLikeListId);
                ew2.notIn("id",helikelist);
                String limit = "limit " + (6-recomlistOne.size());
                ew2.last(limit);
                recomlisttwo = senicInfoService.selectList(ew2);
            }
            recomlistOne.addAll(recomlisttwo);
            recomList = recomlistOne;
        } catch (NullPointerException e) {
            System.out.println("用户没有登录推荐默认列表");
            recomList = senicInfoService.recomList(6);
        }
        return R.ok()
                .put("allInfoList", allinfoList)
                .put("allPlaceInfoList",allPlaceInfoList)
                .put("rankList",rankList)
                .put("recomList",recomList);
    }
    /**
     * 搜索列表
     */
    @RequestMapping("/searchList")
    public R searchList(@RequestBody Map<String,String> params){

        List<SenicInfoEntity> data = senicInfoService.getSearchList(params);
        return R.ok().put("searchList",data);
    }
    /**
     * 景地列表
     */
    @RequestMapping("/senicPlaceList")
    public R toSenicPlaceList(){

        List<String> data = senicInfoService.allPlaceInfoList();
        return R.ok().put("senicPlaceList",data);
    }
    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    public R info(@PathVariable("id") Long id){
		SenicInfoEntity senicInfo = senicInfoService.getSenicInfo(id);

        return R.ok().put("senicInfo", senicInfo);
    }
    /**
     * 地址信息
     */
    @RequestMapping("/placeInfo")
    public R placeInfo(){
		List<String> place_list = senicInfoService.allPlaceInfoList();

        return R.ok().put("data", place_list);
    }
    /**
     * 保存
     */
    @RequestMapping("/save")
    public R save(@RequestBody SenicInfoEntity senicInfo){
        EntityWrapper ew = new EntityWrapper<SenicInfoEntity>();
        ew.eq("senic_name",senicInfo.getSenicName());
        SenicInfoEntity senicOne = senicInfoService.selectOne(ew);
        if (senicOne != null) {
            return R.error("景点重复");
        }
		senicInfoService.insert(senicInfo);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    
    public R update(@RequestBody SenicInfoEntity senicInfo){
		senicInfoService.updateById(senicInfo);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    
    public R delete(@RequestBody Integer[] ids){
		senicInfoService.deleteBatchIds(Arrays.asList(ids));

        return R.ok();
    }
    /**
     * 上传文件
     */
    @RequestMapping("/uploadSenicImg")
    public R uploadFile(@RequestParam("file") MultipartFile senicImgOne) {
    	
    	String res = senicInfoService.SavaImgfile(senicImgOne);
    	return R.ok(res);
    }

}
